using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Matrix.ServiceDiscovery.EntityFrameworkCore;
using Matrix.ServiceDiscovery.Services;
using Matrix.ServiceDiscovery.Caching;
using Matrix.ServiceDiscovery.Permissions;

namespace Matrix.ServiceDiscovery.Application.Tests
{
    [DependsOn(
        typeof(ServiceDiscoveryApplicationModule),
        typeof(ServiceDiscoveryEntityFrameworkCoreModule),
        typeof(ServiceDiscoveryTestModule)
    )]
    public abstract class ServiceDiscoveryApplicationTestBase : AbpIntegratedTest<ServiceDiscoveryApplicationTestModule>
    {
        protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
        {
            options.UseAutofac();
        }

        protected override void AfterAddApplication(IServiceCollection services)
        {
            services.Configure<AbpDbContextOptions>(options =>
            {
                options.Configure(abpDbContextConfigurationContext =>
                {
                    abpDbContextConfigurationContext.DbContextOptions.UseSqlite(abpDbContextConfigurationContext.ConnectionString);
                });
            });

            base.AfterAddApplication(services);
        }
    }

    /// <summary>
    /// ServiceDiscovery测试模块
    /// </summary>
    [DependsOn(
        typeof(ServiceDiscoveryApplicationModule),
        typeof(ServiceDiscoveryEntityFrameworkCoreModule),
        typeof(AbpTestBaseModule),
        typeof(AbpAuthorizationModule)
    )]
    public class ServiceDiscoveryTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpDbContextOptions>(options =>
            {
                options.Configure(abpDbContextConfigurationContext =>
                {
                    abpDbContextConfigurationContext.DbContextOptions.UseSqlite(abpDbContextConfigurationContext.ConnectionString);
                });
            });

            // 配置测试用的服务
            context.Services.AddTransient<IServiceDiscoveryAppService, ServiceDiscoveryAppService>();
            context.Services.AddTransient<ServiceDiscoveryQueryService>();
            context.Services.AddTransient<ServiceDiscoveryCommandService>();
            context.Services.AddTransient<ServiceDiscoveryCacheService>();

            // 添加权限定义
            Configure<AbpPermissionOptions>(options =>
            {
                options.DefinitionProviders.Add<ServiceDiscoveryPermissionDefinitionProvider>();
            });

            // 配置背景任务
            Configure<AbpBackgroundJobOptions>(options =>
            {
                options.IsJobExecutionEnabled = false; // 测试时禁用后台任务
            });
        }
    }

    /// <summary>
    /// 测试用户类
    /// </summary>
    public class TestUser : ICurrentUser
    {
        public Guid? Id { get; } = Guid.NewGuid();
        public string? UserName { get; } = "test-user";
        public string? Name { get; } = "Test User";
        public string? Email { get; } = "test@example.com";
        public string? EmailVerified { get; }
        public string? PhoneNumber { get; }
        public string? PhoneNumberVerified { get; }
        public Guid[]? Roles { get; } = Array.Empty<Guid>();
        public bool? IsActive { get; } = true;
        public Guid? TenantId { get; }
        public string? SurName { get; } = "User";
    }
}